import 'package:flutter/material.dart';

// 点击或者滚动使全部子节点输入框失去焦点
class LoseFocus extends StatelessWidget {
  final Widget child;

  LoseFocus({
    required this.child,
  });

  static void loseFocus() {
    FocusManager.instance.primaryFocus?.unfocus();
  }

  @override
  Widget build(BuildContext context) {
    return NotificationListener(
      onNotification: (Notification notification) {
        if (notification is ScrollNotification) {
          loseFocus();
        }
        return true;
      },
      child: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: loseFocus,
        child: child,
      ),
    );
  }
}
